/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.restclient;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.table.TableRowSorter;



import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Daniel
 */
public class MainWindow extends javax.swing.JFrame {

    private HotelTableModel hotelModel = new HotelTableModel();
    private CustomerTableModel customerModel = new CustomerTableModel();
    static final Logger log = Logger.getLogger(CustomerTableModel.class.getName());

    /**
     * Creates new form MainWindow
     */
    public MainWindow() {       
        initComponents();
        
        hotelTable.setModel(hotelModel);
        customerTable.setModel(customerModel);
        
        this.setLocationRelativeTo(null);
        
        setHotelTableModel();
        setCustomerTableModel();
        
        customerModel.loadData();
        hotelModel.loadData();
    }

    private void setHotelTableModel() {
        TableRowSorter<HotelTableModel> hotelSorter = new TableRowSorter<HotelTableModel>(hotelModel);
        hotelSorter.toggleSortOrder(0);
        hotelTable.setRowSorter(hotelSorter);
        hotelTable.getColumnModel().getColumn(0).setPreferredWidth(100);
        hotelTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        hotelTable.getColumnModel().getColumn(2).setPreferredWidth(100);
        hotelTable.getColumnModel().getColumn(3).setPreferredWidth(100);

    }

    private void setCustomerTableModel() {
        TableRowSorter<CustomerTableModel> customerSorter = new TableRowSorter<CustomerTableModel>(customerModel);
        customerSorter.toggleSortOrder(0);
        customerTable.setRowSorter(customerSorter);
        customerTable.getColumnModel().getColumn(0).setPreferredWidth(40);
        customerTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        customerTable.getColumnModel().getColumn(2).setPreferredWidth(100);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        hotelTable = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        nameHotelField = new javax.swing.JTextField();
        findHotelByNameButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        cityHotelField = new javax.swing.JTextField();
        FindHotelByCityButton = new javax.swing.JButton();
        addHotelButton = new javax.swing.JButton();
        deleteHotelButton = new javax.swing.JButton();
        findAllHotelsButton = new javax.swing.JButton();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        customerTable = new javax.swing.JTable();
        customerSurnameLabel = new javax.swing.JLabel();
        customerSurnameField = new java.awt.TextField();
        findCustomerBySurnameButton = new javax.swing.JButton();
        addCutomerButton = new javax.swing.JButton();
        deleteCustomerButton = new javax.swing.JButton();
        findCustomersByNameButton = new javax.swing.JButton();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Booking Manager - RESTful client");

        hotelTable.setModel(new HotelTableModel());
        jScrollPane1.setViewportView(hotelTable);

        jScrollPane1.setBounds(0, 0, 660, 230);
        jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Name:");
        jLabel2.setBounds(20, 250, 40, 20);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
        nameHotelField.setBounds(90, 250, 70, 22);
        jLayeredPane1.add(nameHotelField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        findHotelByNameButton.setText("Find Hotel by Name");
        findHotelByNameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findHotelByNameButtonActionPerformed(evt);
            }
        });
        findHotelByNameButton.setBounds(190, 250, 150, 25);
        jLayeredPane1.add(findHotelByNameButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("City:");
        jLabel3.setBounds(20, 280, 34, 16);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);
        cityHotelField.setBounds(90, 280, 70, 22);
        jLayeredPane1.add(cityHotelField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FindHotelByCityButton.setText("Find Hotel by City");
        FindHotelByCityButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findHotelByCityButtonActionPerformed(evt);
            }
        });
        FindHotelByCityButton.setBounds(190, 280, 150, 25);
        jLayeredPane1.add(FindHotelByCityButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        addHotelButton.setText("Add Hotel");
        addHotelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addHotelButtonActionPerformed(evt);
            }
        });
        addHotelButton.setBounds(400, 260, 150, 25);
        jLayeredPane1.add(addHotelButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        deleteHotelButton.setText("Delete Hotel");
        deleteHotelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteHotelButtonActionPerformed(evt);
            }
        });
        deleteHotelButton.setBounds(400, 300, 150, 25);
        jLayeredPane1.add(deleteHotelButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        findAllHotelsButton.setText("Find All Hotels");
        findAllHotelsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findAllHotelsButtonActionPerformed(evt);
            }
        });
        findAllHotelsButton.setBounds(190, 310, 150, 25);
        jLayeredPane1.add(findAllHotelsButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTabbedPane1.addTab("Hotels", jLayeredPane1);

        customerTable.setModel(new CustomerTableModel());
        jScrollPane2.setViewportView(customerTable);

        jScrollPane2.setBounds(0, 0, 660, 220);
        jLayeredPane2.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        customerSurnameLabel.setText("Surname");
        customerSurnameLabel.setBounds(50, 250, 70, 20);
        jLayeredPane2.add(customerSurnameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        customerSurnameField.setBounds(120, 250, 110, 20);
        jLayeredPane2.add(customerSurnameField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        findCustomerBySurnameButton.setText("Find customer by surname");
        findCustomerBySurnameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findCustomerBySurnameButtonActionPerformed(evt);
            }
        });
        findCustomerBySurnameButton.setBounds(40, 290, 190, 25);
        jLayeredPane2.add(findCustomerBySurnameButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        addCutomerButton.setText("Add customer");
        addCutomerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCutomerButtonActionPerformed(evt);
            }
        });
        addCutomerButton.setBounds(290, 270, 120, 25);
        jLayeredPane2.add(addCutomerButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        deleteCustomerButton.setText("Delete customer");
        deleteCustomerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteCustomerButtonActionPerformed(evt);
            }
        });
        deleteCustomerButton.setBounds(290, 310, 120, 25);
        jLayeredPane2.add(deleteCustomerButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        findCustomersByNameButton.setText("Find all customers");
        findCustomersByNameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findCustomersByNameButtonActionPerformed(evt);
            }
        });
        findCustomersByNameButton.setBounds(40, 330, 190, 25);
        jLayeredPane2.add(findCustomersByNameButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTabbedPane1.addTab("Customers", jLayeredPane2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(34, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 669, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(50, 50, 50))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(49, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void findHotelByNameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findHotelByNameButtonActionPerformed
        final String field;
        field = nameHotelField.getText();

        RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
            @Override
            public boolean include(RowFilter.Entry entry) {
                String s = (String) entry.getValue(0);
                return s.equals(field);
            }
        };
        TableRowSorter sorter = (TableRowSorter) hotelTable.getRowSorter();
        sorter.setRowFilter(filter);
    }//GEN-LAST:event_findHotelByNameButtonActionPerformed

    private void addHotelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addHotelButtonActionPerformed
        AddHotelDialog addHotelDialog = new AddHotelDialog(hotelTable);
        addHotelDialog.setVisible(true);
    }//GEN-LAST:event_addHotelButtonActionPerformed

    private void deleteHotelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteHotelButtonActionPerformed
        int row = hotelTable.getSelectedRow();

        row = hotelTable.convertRowIndexToModel(row);
        int reply = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this item?", "Confirm deletion", JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION) {
            hotelModel.removeHotel(row);
        }
    }//GEN-LAST:event_deleteHotelButtonActionPerformed

    private void findHotelByCityButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findHotelByCityButtonActionPerformed
        final String field;
        field = cityHotelField.getText();

        RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
            @Override
            public boolean include(RowFilter.Entry entry) {
                String s = (String) entry.getValue(3);
                return s.equals(field);
            }
        };
        TableRowSorter sorter = (TableRowSorter) hotelTable.getRowSorter();
        sorter.setRowFilter(filter);
    }//GEN-LAST:event_findHotelByCityButtonActionPerformed

    private void findAllHotelsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findAllHotelsButtonActionPerformed
        TableRowSorter sorter = (TableRowSorter) hotelTable.getRowSorter();
        sorter.setRowFilter(null);
    }//GEN-LAST:event_findAllHotelsButtonActionPerformed

    private void addCutomerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCutomerButtonActionPerformed
        AddCustomerDialog addCustomerDialog = new AddCustomerDialog(customerTable);
        addCustomerDialog.setVisible(true);
    }//GEN-LAST:event_addCutomerButtonActionPerformed

    private void deleteCustomerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteCustomerButtonActionPerformed
        int row = customerTable.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(jDialog1, "no row to delete");
            return;
        }
        row = customerTable.convertRowIndexToModel(row);
        int reply = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this item?", "Confirm deletion", JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION) {
            customerModel.deleteCustomer(row);
        }
    }//GEN-LAST:event_deleteCustomerButtonActionPerformed

    private void findCustomersByNameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findCustomersByNameButtonActionPerformed
        TableRowSorter sorter = (TableRowSorter) customerTable.getRowSorter();
        sorter.setRowFilter(null);
    }//GEN-LAST:event_findCustomersByNameButtonActionPerformed

    private void findCustomerBySurnameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findCustomerBySurnameButtonActionPerformed
        final String field;
        field = customerSurnameField.getText();

        RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
            @Override
            public boolean include(RowFilter.Entry entry) {
                String surname = (String) entry.getValue(2);
                return surname.equals(field);
            }
        };
        TableRowSorter sorter = (TableRowSorter) customerTable.getRowSorter();
        sorter.setRowFilter(filter);
    }//GEN-LAST:event_findCustomerBySurnameButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton FindHotelByCityButton;
    private javax.swing.JButton addCutomerButton;
    private javax.swing.JButton addHotelButton;
    private javax.swing.JTextField cityHotelField;
    private java.awt.TextField customerSurnameField;
    private javax.swing.JLabel customerSurnameLabel;
    private javax.swing.JTable customerTable;
    private javax.swing.JButton deleteCustomerButton;
    private javax.swing.JButton deleteHotelButton;
    private javax.swing.JButton findAllHotelsButton;
    private javax.swing.JButton findCustomerBySurnameButton;
    private javax.swing.JButton findCustomersByNameButton;
    private javax.swing.JButton findHotelByNameButton;
    private javax.swing.JTable hotelTable;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField nameHotelField;
    // End of variables declaration//GEN-END:variables
}
